﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using myTwitter.BO;
using System.Windows;
using System.Diagnostics;
using myTwitter.CustomControls;

namespace myTwitter.DataTemplateSelectors
{
    public class IMessageTemplateSelector : DataTemplateSelector
    {
        

        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            DataTemplate friendsStatusTemplate = null;
            DataTemplate mentionsTemplate = null;
            DataTemplate directMessageTemplate = null;
            DataTemplate searchResultTemplate = null;

            if(friendsStatusTemplate==null)
                friendsStatusTemplate = ((FrameworkElement)container).FindResource("FriendStatusTemplate") as DataTemplate;

            if(mentionsTemplate ==null)
                mentionsTemplate = ((FrameworkElement)container).FindResource("MentionsTemplate") as DataTemplate;

            if(directMessageTemplate==null)
                directMessageTemplate = ((FrameworkElement)container).FindResource("DirectMessageTemplate") as DataTemplate;

            if (searchResultTemplate == null)
                searchResultTemplate = ((FrameworkElement)container).FindResource("SearchResultTemplate") as DataTemplate;

            if (item is Status)
            {
                Status status = item as Status;
                if (status.InReplyToUserScreenName != GlobalController.Instances.ITwitterProvider.Login && 
                    !status.Text.Contains(string.Format("@{0}",GlobalController.Instances.ITwitterProvider.Login)))
                {
                    /*#region Little hack for Blend ...

                    if (GlobalController.Views.IMessagesControl == null)
                        return _friendsStatusTemplate;

                    #endregion*/

                    return friendsStatusTemplate;
                }
                else
                {
                    /*#region Little hack for Blend ...

                    if (GlobalController.Views.IMessagesControl == null)
                        return _mentionsTemplate;

                    #endregion*/

                    return mentionsTemplate;
                }
            }
            else if (item is DirectMessage)
            {
                /*#region Little hack for Blend ...

                if (GlobalController.Views.IMessagesControl == null)
                    return _directMessageTemplate;

                #endregion*/

                return directMessageTemplate;
            }
            else // Then it must be a search result
            {
               /* #region Little hack for Blend ...

                if (GlobalController.Views.IMessagesControl == null)
                    return _searchResultTemplate;

                #endregion*/

                return searchResultTemplate;
            }
        }
    }
}
